﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKRoomInspection.Model;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKRevitKernel.ElementSearchUtility.LinkDocumentSuite;
using YJKRevitKernel.FormUtility.ModelessFormControlSuite.ModelLayer;
using YJKInspectModelFunc;

namespace YJKRoomInspection.ShowDataForm.ModellessFrame
{
    internal partial class FrontRoomListForm_ModellessNoneSwitch : System.Windows.Forms.Form, IModelessFormNoneSwitch
	{
		public ExternalCommandData CommandData
		{
			get
			{
				return this.m_CommandData;
			}
			private set
			{
				this.m_CommandData = value;
			}
		}

		public IWin32Window RevitHandle
		{
			get
			{
				return this.m_revitHandle;
			}
			private set
			{
				this.m_revitHandle = value;
			}
		}

		public UIDocument UiDoc
		{
			get
			{
				return this.m_uiDoc;
			}
			private set
			{
				this.m_uiDoc = value;
			}
		}

		public List<AdapteRoomResulePacker> LstPackerNeedAddValue
		{
			get
			{
				return this.m_lstPackerNeedAddValue;
			}
			private set
			{
				this.m_lstPackerNeedAddValue = value;
			}
		}

		internal FrontRoomListForm_ModellessNoneSwitch(ExternalCommandData inputCommand, IWin32Window inputHandle, UIDocument inputUidoc, List<AdapteRoomResulePacker> inputValues)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.CommandData = inputCommand;
			this.RevitHandle = inputHandle;
			this.UiDoc = inputUidoc;
			this.m_inputValue.AddRange(inputValues);
			this.InitializeComponent();
			this.InitSpaceTree(this.m_inputValue);
		}

		public bool IfBreak
		{
			get
			{
				return this.m_bIfBreak;
			}
		}

		public bool IfContinue
		{
			get
			{
				return this.m_bIfContinue;
			}
		}

		public bool IfManualEsc
		{
			get
			{
				return this.m_bIfManuEsc;
			}
		}

		public void CloseForm()
		{
			if (!base.IsDisposed)
			{
				base.Close();
			}
		}

		public void ResetContinueBreak()
		{
			this.m_bIfContinue = false;
			this.m_bIfBreak = true;
			this.m_bIfManuEsc = true;
		}

		public void SendEscMessageToRevitAndClearSelect()
		{
			this.m_useBeforeCotinueMethod = null;
			RevitVersionFuncs.ClearSelection(this.UiDoc.Selection);
			if (this.IfThisFormActive())
			{
				Utility.SendEscToHandle(this.RevitHandle);
			}
		}

		public void ShowForm(IWin32Window parentForm = null)
		{
			if (this.RevitHandle != null)
			{
				base.Show(this.RevitHandle);
				return;
			}
			base.Show();
		}

		public void DoBeforeContinue()
		{
			if (this.m_useBeforeCotinueMethod != null)
			{
				this.m_useBeforeCotinueMethod();
			}
		}

		public void ChangeFilledRegionVisible(List<int> lstFilledRegionIds)
		{
			Document document = this.UiDoc.Document;
			Transaction transaction = new Transaction(document, "changeFilledRegionVisible");
			List<ElementId> list = new List<ElementId>();
			foreach (int num in lstFilledRegionIds)
			{
				list.Add(new ElementId(num));
			}
			if (list.Count == 0 && (list == null || this.m_lstFrontElmentId.Count == 0))
			{
				return;
			}
			try
			{
				transaction.Start();
				if (this.m_frontViewPlan != null && this.m_lstFrontElmentId != null && this.m_lstFrontElmentId.Count > 0)
				{
					this.m_frontViewPlan.HideElements(this.m_lstFrontElmentId);
				}
				if (list.Count > 0)
				{
					document.ActiveView.UnhideElements(list);
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				if (transaction.HasStarted())
				{
					transaction.RollBack();
				}
			}
			finally
			{
				this.m_frontViewPlan = (document.ActiveView as ViewPlan);
				this.m_lstFrontElmentId = list;
			}
		}

		private List<AdapteRoomResulePacker> GetNowPackersInTreeView()
		{
			List<AdapteRoomResulePacker> list = new List<AdapteRoomResulePacker>();
			foreach (object obj in this.treeView_frontRoomTree.Nodes)
			{
				TreeNode inputNode = (TreeNode)obj;
				list.AddRange(this.GetNowPackersInTreeNode(inputNode));
			}
			return list;
		}

		private List<AdapteRoomResulePacker> GetNowPackersInTreeNode(TreeNode inputNode)
		{
			List<AdapteRoomResulePacker> list = new List<AdapteRoomResulePacker>();
			if (inputNode.Tag is AdapteRoomResulePacker)
			{
				list.Add(inputNode.Tag as AdapteRoomResulePacker);
			}
			foreach (object obj in inputNode.Nodes)
			{
				TreeNode inputNode2 = (TreeNode)obj;
				list.AddRange(this.GetNowPackersInTreeNode(inputNode2));
			}
			return list;
		}

		private void InitSpaceTree(List<AdapteRoomResulePacker> lstRooms)
		{
			IEnumerable<IGrouping<int, AdapteRoomResulePacker>> enumerable = from k in (from n in lstRooms
			where n.UseLevel != null
			select n).Distinct<AdapteRoomResulePacker>().ToList<AdapteRoomResulePacker>()
			group k by k.UseLevel.Id.IntegerValue;
			this.treeView_frontRoomTree.Nodes.Clear();
			foreach (IGrouping<int, AdapteRoomResulePacker> grouping in enumerable)
			{
				int num = grouping.Count<AdapteRoomResulePacker>();
				string text = grouping.ElementAt(0).UseLevel.Name;
				text = string.Format("{0}({1})", text, num - 1);
				TreeNode treeNode = this.treeView_frontRoomTree.Nodes.Add(grouping.Key.ToString(), text);
				AdapteRoomResulePacker tag = null;
				foreach (AdapteRoomResulePacker adapteRoomResulePacker in grouping)
				{
					if (adapteRoomResulePacker.IfOnlyLevel())
					{
						tag = adapteRoomResulePacker;
					}
					else
					{
						treeNode.Nodes.Add(adapteRoomResulePacker.GetElementId().ToString(), adapteRoomResulePacker.GetElementDes()).Tag = adapteRoomResulePacker;
					}
				}
				treeNode.Tag = tag;
			}
			this.treeView_frontRoomTree.ExpandAll();
		}

		private void treeView_frontRoomTree_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			this.m_bIfContinue = true;
			this.m_bIfBreak = false;
			this.m_bIfManuEsc = false;
			this.SendEscMessageToRevitAndClearSelect();
			this.m_useBeforeCotinueMethod = new FrontRoomListForm_ModellessNoneSwitch.BeforeContinueMethod(this.LocationMethod);
		}

		private void LocationMethod()
		{
			TreeNode selectedNode = this.treeView_frontRoomTree.SelectedNode;
			ViewPlan viewPlan = null;
			new List<ViewPlan>();
			Document document = this.UiDoc.Document;
			List<int> lstAllRoomId = new List<int>();
			List<int> list = new List<int>();
			ElementSet elementSet = new ElementSet();
			List<Element> list2 = new List<Element>();
			if (this.IfIsLevelNode(selectedNode))
			{
				this.ChangeNowViewPlan(selectedNode, ref viewPlan, document);
				IEnumerator enumerator = selectedNode.Nodes.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						TreeNode oneNode = (TreeNode)obj;
						this.AdpatNodeInformation(document, lstAllRoomId, list, elementSet, list2, oneNode);
					}
					goto IL_E8;
				}
			}
			if (selectedNode.Tag is AdapteRoomResulePacker && !(selectedNode.Tag as AdapteRoomResulePacker).IfOnlyLevel())
			{
				if (this.IfIsLevelNode(selectedNode.Parent))
				{
					this.ChangeNowViewPlan(selectedNode.Parent, ref viewPlan, document);
				}
				this.AdpatNodeInformation(document, lstAllRoomId, list, elementSet, list2, selectedNode);
			}
			IL_E8:
			this.ChangeFilledRegionVisible(list);
			if (elementSet.Size > 0)
			{
				this.UiDoc.ShowElements(elementSet);
				RevitVersionFuncs.SetSelection(this.UiDoc.Selection, list2);
				this.UiDoc.RefreshActiveView();
			}
		}

		private bool IfIsLevelNode(TreeNode nowNode)
		{
			return nowNode.Tag is AdapteRoomResulePacker && (nowNode.Tag as AdapteRoomResulePacker).IfOnlyLevel();
		}

		private List<ViewPlan> ChangeNowViewPlan(TreeNode nowNode, ref ViewPlan wantUseView, Autodesk.Revit.DB.Document useDoc)
		{
			List<ViewPlan> list;
			if (LinkDocumentUtility.TryGetViewPlanNearElvation(useDoc, (nowNode.Tag as AdapteRoomResulePacker).UseLevel.Elevation, out list, 0.0) && list.Count > 0)
			{
				wantUseView = list[0];
				this.UiDoc.ActiveView = wantUseView;
			}
			return list;
		}

		private void AdpatNodeInformation(Autodesk.Revit.DB.Document useDoc, List<int> lstAllRoomId, List<int> lstAllFilledRegionId, ElementSet useSet, List<Element> lstElement, TreeNode oneNode)
		{
			Element element = null;
			if (oneNode.Tag is AdapteRoomResulePacker)
			{
				element = useDoc.GetElement(new ElementId((oneNode.Tag as AdapteRoomResulePacker).GetElementId()));
			}
			if (element != null)
			{
				useSet.Insert(element);
				lstElement.Add(element);
			}
			if (element is FilledRegion)
			{
				lstAllFilledRegionId.Add(element.Id.IntegerValue);
				return;
			}
			lstAllRoomId.Add(element.Id.IntegerValue);
		}

		private void btn_SelectAll_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.treeView_frontRoomTree.Nodes.Count; i++)
			{
				if (this.treeView_frontRoomTree.Nodes[i].Nodes.Count == 0)
				{
					this.treeView_frontRoomTree.Nodes[i].Checked = true;
				}
				for (int j = 0; j < this.treeView_frontRoomTree.Nodes[i].Nodes.Count; j++)
				{
					this.treeView_frontRoomTree.Nodes[i].Nodes[j].Checked = true;
				}
			}
		}

		private void btn_NoSelectAll_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.treeView_frontRoomTree.Nodes.Count; i++)
			{
				if (this.treeView_frontRoomTree.Nodes[i].Nodes.Count == 0)
				{
					this.treeView_frontRoomTree.Nodes[i].Checked = false;
				}
				for (int j = 0; j < this.treeView_frontRoomTree.Nodes[i].Nodes.Count; j++)
				{
					this.treeView_frontRoomTree.Nodes[i].Nodes[j].Checked = false;
				}
			}
		}

		private void btn_ReverseSelect_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.treeView_frontRoomTree.Nodes.Count; i++)
			{
				if (this.treeView_frontRoomTree.Nodes[i].Nodes.Count == 0)
				{
					this.treeView_frontRoomTree.Nodes[i].Checked = !this.treeView_frontRoomTree.Nodes[i].Checked;
				}
				for (int j = 0; j < this.treeView_frontRoomTree.Nodes[i].Nodes.Count; j++)
				{
					this.treeView_frontRoomTree.Nodes[i].Nodes[j].Checked = !this.treeView_frontRoomTree.Nodes[i].Nodes[j].Checked;
				}
			}
		}

		private void btn_Delete_Click(object sender, EventArgs e)
		{
			List<AdapteRoomResulePacker> list = new List<AdapteRoomResulePacker>();
			for (int i = 0; i < this.treeView_frontRoomTree.Nodes.Count; i++)
			{
				bool flag = true;
				flag = (flag && this.treeView_frontRoomTree.Nodes[i].Checked);
				for (int j = 0; j < this.treeView_frontRoomTree.Nodes[i].Nodes.Count; j++)
				{
					flag = (flag && this.treeView_frontRoomTree.Nodes[i].Nodes[j].Checked);
					if (!this.treeView_frontRoomTree.Nodes[i].Nodes[j].Checked && this.treeView_frontRoomTree.Nodes[i].Nodes[j].Tag is AdapteRoomResulePacker)
					{
						list.Add(this.treeView_frontRoomTree.Nodes[i].Nodes[j].Tag as AdapteRoomResulePacker);
					}
				}
				if (!flag && this.treeView_frontRoomTree.Nodes[i].Tag is AdapteRoomResulePacker)
				{
					list.Add(this.treeView_frontRoomTree.Nodes[i].Tag as AdapteRoomResulePacker);
				}
			}
			this.InitSpaceTree(list);
		}

		private void btn_Add_Click(object sender, EventArgs e)
		{
			this.m_bIfContinue = true;
			this.m_bIfBreak = false;
			this.m_bIfManuEsc = false;
			this.SendEscMessageToRevitAndClearSelect();
			this.m_useBeforeCotinueMethod = new FrontRoomListForm_ModellessNoneSwitch.BeforeContinueMethod(this.ManulaAddMethod);
		}

		private void ManulaAddMethod()
		{
			List<AdapteRoomResulePacker> list = this.GetNowPackersInTreeView();
			ManualAddFrontRoomForm_ModellessVersion inputForm = new ManualAddFrontRoomForm_ModellessVersion(this.RevitHandle, this.UiDoc);
			ManualAddFrontRoomStackFactory inputFactory = new ManualAddFrontRoomStackFactory(this.CommandData, inputForm);
			ManualAddFrontRoomModelessManger manualAddFrontRoomModelessManger = new ManualAddFrontRoomModelessManger(inputForm, inputFactory);
			base.Visible = false;
			manualAddFrontRoomModelessManger.DoLoopWork();
			list.AddRange(manualAddFrontRoomModelessManger.LstAdaptResult);
			list = list.Distinct<AdapteRoomResulePacker>().ToList<AdapteRoomResulePacker>();
			base.Visible = true;
			this.InitSpaceTree(list);
		}

		private void btn_CancelBtn_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btn_SaveBtn_Click(object sender, EventArgs e)
		{
			List<AdapteRoomResulePacker> collection = (from n in this.GetNowPackersInTreeView()
			where !n.IfOnlyLevel()
			select n).ToList<AdapteRoomResulePacker>();
			this.LstPackerNeedAddValue.AddRange(collection);
			base.Close();
		}

		private bool IfThisFormActive()
		{
			if (this.Focused)
			{
				return true;
			}
			foreach (object obj in base.Controls)
			{
				if (obj is System.Windows.Forms.Control && (obj as System.Windows.Forms.Control).Focused)
				{
					return true;
				}
			}
			return false;
		}

		protected override bool ProcessKeyEventArgs(ref Message m)
		{
			int num = 256;
			int num2 = 27;
			if (m.Msg == num && m.WParam.ToInt32() == num2)
			{
				base.Close();
			}
			return base.ProcessKeyEventArgs(ref m);
		}

		private ExternalCommandData m_CommandData;

		private IWin32Window m_revitHandle;

		private UIDocument m_uiDoc;

		private List<ElementId> m_lstFrontElmentId = new List<ElementId>();

		private List<AdapteRoomResulePacker> m_inputValue = new List<AdapteRoomResulePacker>();

		private List<AdapteRoomResulePacker> m_lstPackerNeedAddValue = new List<AdapteRoomResulePacker>();

		private FrontRoomListForm_ModellessNoneSwitch.BeforeContinueMethod m_useBeforeCotinueMethod;

		private ViewPlan m_frontViewPlan;

		private delegate void BeforeContinueMethod();
	}
}
